package com.wenshi.web.controller.device;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wenshi.common.core.domain.AjaxResult;
import com.wenshi.common.core.domain.R;
import com.wenshi.system.domain.vo.DeviceVo;
import com.wenshi.system.domain.vo.request.LoginRequest;
import com.wenshi.system.domain.vo.request.ModbusRequest;
import com.wenshi.system.service.GetDeviceStatusService;
import com.wenshi.system.service.ModbusMappingService;
import com.wenshi.web.netty.ModbusClient;
import com.wenshi.web.socket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.wenshi.system.init.InitMappingMap.*;

@RestController
@RequestMapping("/modbus")
@Slf4j
public class ModbusController {
    @Autowired
    private ModbusMappingService modbusMappingService;
    @Autowired
    private GetDeviceStatusService getDeviceStatusService;
    @Autowired
    private WebSocket webSocketServerPlayBack;
    @Autowired
    private ModbusClient modbusClient;
    @Autowired
    private RequestInfoService requestInfoService;


    @GetMapping("/mott")
    @CrossOrigin
    public R testMod(String deviceIp) throws InterruptedException, JsonProcessingException {

        ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
        DeviceVo deviceVo = new DeviceVo();
        deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
        deviceVo.setDeviceIp(deviceIp);
        log.info("数据:{}", deviceVo);
        return R.ok(deviceVo);
    }

    @PostMapping("/api/login/user")
    public void login(@RequestBody LoginRequest loginRequest) {


    }

        @PostMapping("/test")
    public R testMod(@RequestBody ModbusRequest modbusRequest) {
        String deviceIp = modbusRequest.getDeviceIp();
        int[][] address = modbusRequest.getAddress();
        boolean test = getDeviceStatusService.test(deviceIp,address);
        if (test) {
            DeviceVo deviceVo = new DeviceVo();
            ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
            deviceVo.setDeviceIp(deviceIp);
            deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
            log.info("数据:{}", deviceVo.toString());
            return R.ok(deviceVo);

        }
        return R.fail();
    }

    @GetMapping("/getRunPageStatus")
    @CrossOrigin
    public R getRunPageStatus(String deviceIp) throws InterruptedException {
//        modbusAndResultMap.clear();
//        nameAndResultMap.clear();
        boolean runPageStatus = getDeviceStatusService.getRunPageStatus(deviceIp);
        if (runPageStatus) {
            DeviceVo deviceVo = new DeviceVo();
            ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
            deviceVo.setDeviceIp(deviceIp);
            deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
            log.info("数据:{}", deviceVo.toString());
            return R.ok(deviceVo);

        }
        return R.fail();
    }

    @GetMapping("/web")
    public void test() throws IOException {
        webSocketServerPlayBack.sendAllMsgToWebSocket("dasdsdasdsasad");
        log.info("");
    }

    @GetMapping("/getRunPageStatus1")
    @CrossOrigin
    public R getRunPageStatus1(String deviceIp,int port) {
        modbusAndResultMap.clear();
        nameAndResultMap.clear();

        modbusClient.doConnect(deviceIp,port);

        getDeviceStatusService.getRunPageStatus(deviceIp);
        DeviceVo deviceVo = new DeviceVo();
//        Map<String, Integer> stringIntegerMap = resultAndDescMap.get(deviceId);
//        deviceVo.setDeviceId(deviceId);
//        deviceVo.setDeviceData(stringIntegerMap);
        log.info("数据:{}", deviceVo);
        return R.ok(deviceVo);
    }

    @PostMapping("/on")
    public AjaxResult startDevice(@RequestBody ModbusRequest modbusRequest) {
        String deviceIp = modbusRequest.getDeviceIp();
        int[][] address = modbusRequest.getAddress();
        boolean test = getDeviceStatusService.onDevice(deviceIp,address);
        if (test) {
            DeviceVo deviceVo = new DeviceVo();
            ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
            deviceVo.setDeviceIp(deviceIp);
            deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
            log.info("数据:{}", deviceVo.toString());
            return AjaxResult.success(deviceVo);

        }
        return AjaxResult.error();
    }
    @PostMapping("/off")
    public AjaxResult stopDevice(@RequestBody ModbusRequest modbusRequest) {
        String deviceIp = modbusRequest.getDeviceIp();
        int[][] address = modbusRequest.getAddress();
        boolean test = getDeviceStatusService.offDevice(deviceIp,address);
        if (test) {
            DeviceVo deviceVo = new DeviceVo();
            ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
            deviceVo.setDeviceIp(deviceIp);
            deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
            log.info("数据:{}", deviceVo.toString());
            return AjaxResult.success(deviceVo);
        }
        return AjaxResult.error();
    }
//READ_HOLDING_REGISTERS
    @PostMapping("/input")
    public AjaxResult inputHoldingRegisters(@RequestBody ModbusRequest modbusRequest){
        boolean result = getDeviceStatusService.inputHoldingRegister(modbusRequest);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("/testmodbus")
    public AjaxResult testmodbus(@RequestBody ModbusRequest modbusRequest){
        modbusAndResultMap.clear();
        nameAndResultMap.clear();

        boolean test = getDeviceStatusService.getTest(modbusRequest);
        String deviceIp = modbusRequest.getDeviceIp();
        if (test) {
            DeviceVo deviceVo = new DeviceVo();
            ConcurrentHashMap<String, Integer> stringIntegerConcurrentHashMap = resultAndDescMap.get(deviceIp);
            deviceVo.setDeviceIp(deviceIp);
            deviceVo.setDeviceData(stringIntegerConcurrentHashMap);
            log.info("数据:{}", deviceVo.toString());
            return AjaxResult.success(deviceVo);
        }
        return AjaxResult.error();
    }
    @PostMapping("send")
    public R test(@RequestBody TestFA testFA){
//       List<TestDto> randomObjectList = testFA.getRandomObjectList();
//        for (TestDto testDto : randomObjectList) {
//            System.out.println(testDto.getMax());
//            System.out.println(testDto.getIndex());
//            System.out.println(testDto.getDecimalNum());
//            System.out.println(testDto.getMin());
//        }
        return R.ok();
    }
    @PostMapping("/sendMsg")
    public Map<String,String> test1(HttpServletRequest request, @RequestBody TestResult testResult){
        TestFA data = testResult.getData();
        System.out.println(data.getWendu());
            System.out.println(data.getShidu());

        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setUrl(request.getRemoteAddr().toString());
        requestInfo.setMethod(request.getMethod());

        requestInfoService.saveRequestInfo(requestInfo);
        Map<String, String> objectObjectMap = new HashMap<>();
////        objectObjectMap.put("result","222");

        return objectObjectMap;
    }
    private final RestTemplate restTemplate = new RestTemplate();
    @PostMapping("/receive")
    public void receiveRequest(@RequestBody TestDto resultRequest) {

        List<RequestInfo> requestInfoList = requestInfoService.getAllRequestInfo();
        for (RequestInfo requestInfo : requestInfoList) {

            if ("POST".equalsIgnoreCase(requestInfo.getMethod())) {


                restTemplate.postForObject(requestInfo.getUrl(), resultRequest, String.class);
            } else if ("GET".equalsIgnoreCase(requestInfo.getMethod())) {
                restTemplate.getForObject(requestInfo.getUrl(), String.class);
            }
        }
    }

}